using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TAC;

namespace MenuTeste
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //Menu Objects
        Texture2D texture, texture1, texture2, texture3, texture4;
        private Boolean menu = true;
        private int state = 1;
        KeyboardState oldState;

        // 3D objects
        Arena arena;
        NPC npc, pc;
        Facil IA_NPC, IA_PC, current;
        Model myModel;
        Model ModelArena;
        List<NPC> NPCList;
        private Boolean Teste3D = false;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
            //graphics.ToggleFullScreen();
            IsMouseVisible = true;
            oldState = Keyboard.GetState();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            NPCList = new List<NPC>();

            arena = Arena.GetInstance(graphics.GraphicsDevice);
            pc = new NPC(graphics.GraphicsDevice, arena.mapa.personagens[0]);
            npc = new NPC(graphics.GraphicsDevice, arena.mapa.personagens[1]);

            NPCList.Add(pc);
            NPCList.Add(npc);
            
            IA_NPC = new Facil(ref npc, ref arena.mapa);
            IA_PC = new Facil(ref pc, ref arena.mapa);

            current = IA_NPC;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Menu content
            texture1 = Content.Load<Texture2D>("menu - single player");
            texture = texture1;
            texture2 = Content.Load<Texture2D>("menu - characters");
            texture3 = Content.Load<Texture2D>("menu - credits");
            texture4 = Content.Load<Texture2D>("menu - exit");

            //3D content
            myModel = Content.Load<Model>("static player");
            ModelArena = Content.Load<Model>("plane");
            arena.LoadContent(ref ModelArena, Content);

            npc.LoadContent(myModel);

            pc.LoadContent(Content.Load<Model>("npc"));
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            texture.Dispose();
            texture1.Dispose();
            texture2.Dispose();
            texture3.Dispose();
            texture4.Dispose();

            arena.UnloadContent();
            npc.UnloadContent();
            pc.UnloadContent();
        }

        int coin = 0;
        private void Logic()
        {
            if (current.controlado.character.isDead())
            {
                menu = true;
                Teste3D = false;
                // Textura Game Over
                Exit();
            }
            else
            {
                if (current.controlado.IsDone())
                {   
                    coin++;

                    current.controlado.Reset();
                    if (coin % 2 == 0)
                    {
                        current = IA_PC;
                    }
                    else
                    {
                        current = IA_NPC;
                    }
                }
                else
                {
                    if (!current.controlado.AlreadyDecided())
                    {
                        current.controlado.Decided();
                        /*if (coin % 3 == 0)
                        {
                            current.controlado.Move(coin / 10, coin % 10);
                        }
                        else
                            current.controlado.SuperAttacked();
                        current.controlado.Atacked();*/
                        current.comportamento();

                    }
                }
            }
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {

            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Escape))
                Exit();

            if (menu)
            {
                if (state == 1)
                {
                    if (keyboard.IsKeyDown(Keys.Enter))
                    {
                        Teste3D = true;
                        //Logic();
                        current.controlado.Update(gameTime);
                        menu = false;
                    }
                    if (keyboard.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up))
                    {
                        state = 4;
                        texture = texture4;
                    }
                    if (keyboard.IsKeyDown(Keys.Down)&& !oldState.IsKeyDown(Keys.Down))
                    {
                        state = 2;
                        texture = texture2;
                    }
                } else
                if (state == 2)
                {
                    if (keyboard.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up))
                    {
                        state = 1;
                        texture = texture1;
                    }
                    if (keyboard.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.Down))
                    {
                        state = 3;
                        texture = texture3;
                    }
                } else
                if (state == 3)
                {
                    if (keyboard.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up))
                    {
                        state = 2;
                        texture = texture2;
                    }
                    if (keyboard.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.Down))
                    {
                        state = 4;
                        texture = texture4;
                    }
                } else
                if (state == 4)
                {
                    if (keyboard.IsKeyDown(Keys.Enter))
                        Exit();
                    if (keyboard.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up))
                    {
                        state = 3;
                        texture = texture3;
                    }
                    if (keyboard.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.Down))
                    {
                        state = 1;
                        texture = texture1;
                    }
                }
            }
            oldState = keyboard;

            if (Teste3D)
            {
                Logic();

                current.controlado.Update(gameTime);
                pc.Update(gameTime);
                npc.Update(gameTime);
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            if (menu)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(texture, new Rectangle(0, 0, graphics.GraphicsDevice.DisplayMode.Width, graphics.GraphicsDevice.DisplayMode.Height), Color.Aquamarine);
                spriteBatch.End();
            }
            
            if (Teste3D)
            {
                //3D drawing
                arena.Draw(ref spriteBatch);
                pc.Draw();
                npc.Draw();
            }
            base.Draw(gameTime);
        }

        void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;

        }
    }
}
